package org.xiaov.codeautocreator.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import org.xiaov.codeautocreator.common.request.TableListRequest;
import org.xiaov.codeautocreator.entity.ColumnEntity;
import org.xiaov.codeautocreator.entity.TableEntity;
import org.xiaov.codeautocreator.mapper.AutoCodeMapper;
import org.xiaov.codeautocreator.service.CodeAutoCreatorService;
import org.xiaov.codeautocreator.util.AutoCodeUtils;

import javax.annotation.Resource;
import java.io.ByteArrayOutputStream;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipOutputStream;

/**
 * <p>
 * 描述：
 * </p>
 *
 * @author xiaovcloud
 * @since 2021/9/5 21:21
 */
@Service
public class CodeAutoCreatorServiceImpl implements CodeAutoCreatorService {

    @Resource
    AutoCodeMapper autoCodeMapper;

    @Override
    public List<TableEntity> queryList(TableListRequest request) {
        Page<TableEntity> tableEntityPage = new Page<>(
                Integer.parseInt(request.getCurrent()), Integer.parseInt(request.getSize()));
        String tableName = request.getTableName();
        return autoCodeMapper.queryList(tableEntityPage, tableName);
    }

    @Override
    public boolean autocode(String[] tableNames) {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        ZipOutputStream zip = new ZipOutputStream(outputStream);
        for (String tableName : tableNames) {
            //查询表信息
            TableEntity table = autoCodeMapper.queryTable(tableName);
            //查询列信息
            List<ColumnEntity> columns = autoCodeMapper.queryColumns(tableName);
            //生成代码
            AutoCodeUtils.autocode(table, columns, zip);
        }
        return true;
    }
}
